package com.wg.msshopsp.backendmanagement.shop.service;

import com.wg.msshopsp.backendmanagement.shop.entity.ShopP;
import com.wg.msshopsp.backendmanagement.shop.mapper.ShopPMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopPMapper shopPMapper; // 注入Mapper

    @Override
    public List<ShopP> getShopsWithProductsByUserId(Integer userId) {
        // 校验参数（简单示例，实际可根据业务扩展）
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        // 调用Mapper查询数据
        return shopPMapper.getShopsByUserId(userId);
    }



    // 新增店铺
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addShop(ShopP shop) {
        // 参数校验
        if (shop.getUserId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        if (shop.getShopName() == null || shop.getShopName().trim().isEmpty()) {
            throw new IllegalArgumentException("店铺名称不能为空");
        }
        // 默认为营业状态
        if (shop.getStatus() == null) {
            shop.setStatus(0); // 0=营业中，1=已关闭
        }
        // 插入数据库
        shopPMapper.insert(shop);
        return shop.getId();
    }

    // 更新店铺
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShop(ShopP shop) {
        // 参数校验
        if (shop.getId() == null) {
            throw new IllegalArgumentException("店铺ID不能为空");
        }
        // 校验店铺是否存在
        ShopP existingShop = shopPMapper.selectById(shop.getId());
        if (existingShop == null) {
            throw new IllegalArgumentException("店铺不存在，ID：" + shop.getId());
        }
        // 执行更新
        shopPMapper.update(shop);
    }

    // 删除店铺
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteShop(Integer shopId) {
        // 参数校验
        if (shopId == null) {
            throw new IllegalArgumentException("店铺ID不能为空");
        }
        // 校验店铺是否存在
        ShopP shop = shopPMapper.selectById(shopId);
        if (shop == null) {
            throw new IllegalArgumentException("店铺不存在，ID：" + shopId);
        }
        // 执行删除（注意：需确保数据库中shop表与product表的外键设置了级联删除，或先删除商品）
        shopPMapper.deleteById(shopId);
    }

    @Override
    public ShopP getShopById(Integer shopId) {
        if (shopId == null) {
            throw new IllegalArgumentException("店铺ID不能为空");
        }
        return shopPMapper.selectById(shopId);
    }



    @Override
    public List<ShopP> getShopsWithProductsByCondition(Integer userId, String productName, String productType, String productStatus) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        // 调用mapper的带条件查询方法
        return shopPMapper.getShopsByUserIdWithCondition(userId, productName, productType, productStatus);
    }
}
